package edu.spec.wechat.controller;

import edu.spec.pms.model.dto.MvcReturn;
import edu.spec.wechat.kit.SecurityKit;
import edu.spec.wechat.model.WxJsConfig;
import edu.spec.wechat.web.WechatContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;

/**
 * Created by MadFrog on 2016/11/22.
 */
@Controller
public class IndexController {

    @RequestMapping("/studentIndex")
    public String index(){
        return "student/index";
    }

    @ResponseBody
    @RequestMapping("/getWechatJsConfig")
    public Object getWechatJsConfig(){
        // TODO url
        String jsapi_ticket = "jsapi_ticket=" + WechatContext.getInstance().getJsApiTicket();
        String noncestr = "noncestr=" + "xiong222";
        String timestamp = "timestamp=" + String.valueOf(System.currentTimeMillis() / 1000);
        String url = "url=" + "http://madfrog.51vip.biz/pms/studentIndex";
        String[] arrs = {url, jsapi_ticket, noncestr, timestamp};
        Arrays.sort(arrs);
        StringBuffer sb = new StringBuffer();
        for (String a : arrs){
            sb.append("&" + a);
        }
        System.out.println("arrs:" + sb.toString().substring(1));
        String signature = SecurityKit.sha1(sb.toString().substring(1));
        WxJsConfig cfg = new WxJsConfig(WechatContext.getInstance().getAppID(), timestamp, noncestr, signature);
        return new MvcReturn(true, cfg);
    }

}
